#include<iostream>
using namespace std;

template<typename T>
class Allocator
{
public:
    T* allocator(size_t size)//负责内存开辟,size个数量
    {
        return (T *)malloc(sizeof(T) * size);
    }
    void deallocator(T* p)//内存释放
    {
        free(p);
    }
    void construct(T* p,const T& val)//对象创建
    {
        // 更多new初始化方法 https://blog.csdn.net/qq_45041871/article/details/132251733
        new (p) T(val);//使用的是定位new，将val传到地址为p的空间去，p地址指向val
    }
    void destroy(T *p)//对象析构
    {
        p->~T();
    }
};
template<typename T,typename Alloc=Allocator<T>>
class Vector
{
public:
    Vector(int size=5)
    {
        // 需要将内存开辟和对象构造分开处理
        // _first = new T[size];
        _first = _allocator.allocator(size);//只开辟空间
        _last = _first;
        _end = _first + size;//这就相当于指针遍历移动
    }
    ~Vector()
    {
        // 析构容器中有效的元素，然后释放_first指针所指向的内存
        // delete[] _first;
        for (T *p = _first; p != _last;p++)
        {
            _allocator.destroy(p);
        }
        _allocator.deallocator(_first);
        _first = _last = _end = nullptr;
    }
    Vector(const Vector<T> &src)
    {
        int size = src._end - src._first;
        // _first = new T[size];
        _first = _allocator.allocator(size);
        for (int i = 0; i < src._last - src._first; i++)
        {
            // _first[i] = src._first[i];
            _allocator.construct(_first + i, src._first[i]);
        }
        _last = _first + src._last - src._first;
        _end = _first + size;
    }
    Vector<T>& operator=(const Vector<T>&src)
    {
        if(this==&src)
            return *this;
        // delete[] _first;
        for (T *p = _first; p != _last; p++)
        {
            _allocator.destroy(p);
        }
        _allocator.deallocator(_first);

        int size = src._end - src._first;
        _first = new T[size];
        for (int i = 0; i < src._last - src._first; i++)
        {
            // _first[i] = src._first[i];
            _allocator.construct(_first + i, src._first[i]);
        }
        _last = _first + src._last - src._first;
        _end = _first + size;
        return *this;
    }
    void push_back(T& val)
    {
        if(full())
            expand();
        // int pos = _last - _first;
        // _first[pos] = val;
        // _last += 1;
        // *_last++ = val;//和上面应该是一样的
        _allocator.construct(_last,val);
        _last++;
    }
    void pop_back()
    {
        if(empty())
            return;
        _last--;
        _allocator.destroy(_last);
    }
    T back()const
    {
        return *(_last - 1);
    }
    bool full() const { return _last == _end; }
    bool empty() const { return _last == _first; }

private:
    T *_first;//指向数组起始的位置
    T *_last;//指向数组中有效元素的后继位置
    T *_end;//指向数组空间的后继位置

    Alloc _allocator;//定义容器的空间配置器
    void expand()
    {
        int size = _end - _first;
        // T *temp = new T[size * 2];
        T *temp = _allocator.allocator(size * 2);
        for (int i = 0; i < size; i++)
        {
            // temp[i] = _first[i];
            _allocator.construct(temp + i, _first[i]);
        }
        // delete[] _first;
        for (T *p = _first; p != _last; p++)
        {
            _allocator.destroy(p);
        }
        _allocator.deallocator(_first);

        _first = temp;
        _last = _first+size;
        _end = _last + size;
    }
};

class Test
{
public:
    Test() { cout << "Test()" << endl; }
    ~Test() { cout << "~Test()" << endl; }
    Test(const Test& val) { cout << "Test(const Test& val)" << endl; }
};

int main()
{
    // 使用对象类型，那么就会在初始化v对象时调用new Test[10]，会自动创建10个Test对象，就会存在错误
    Vector<Test> v;

    Test t1, t2, t3;
    v.push_back(t1);
    v.push_back(t2);
    v.push_back(t3);
    cout << "----------------------"<<endl;
    v.pop_back();
    cout << "----------------------"<<endl;

    /*
    Vector<int> v;
    for (int i = 0; i < 20;i++)
    {
        v.push_back(i);
    }
    while(!v.empty())
    {
        cout << v.back() << " ";
        v.pop_back();
    }
    */
    return 0;
}